package com.bzkj.basicinfo.service.impl.ncc;

import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.bzkj.basicinfo.domain.ncc.NccBasicMaterialstock;
import com.bzkj.basicinfo.domain.ncc.dto.NccBasicMaterialstockDto;
import com.bzkj.basicinfo.mapper.NccBasicMaterialstockMapper;
import com.bzkj.basicinfo.service.ncc.NccBasicMaterialstockService;
import com.bzkj.basicinfo.utils.DataBatchProcessorUtil;
import com.bzkj.basicinfo.utils.HttpClientUtils;
import com.bzkj.basicinfo.utils.UniqueIdentifierGenerator;
import com.bzkj.common.core.domain.AjaxResult;
import com.bzkj.common.core.domain.R;
import com.bzkj.common.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author wangzhichao
 * @description 针对表【ncc_basic_materialstock(ncc物料库存信息)】的数据库操作Service实现
 * @createDate 2024-12-11 14:51:20
 * 物料库存信息：15
 */
@Slf4j
@Service("nccBasicMaterialstockServiceImpl")
public class NccBasicMaterialstockServiceImpl implements NccBasicMaterialstockService {
    @Autowired
    private NccBasicMaterialstockMapper nccBasicMaterialstockMapper;

    @Autowired
    private UniqueIdentifierGenerator uniq;

    @Autowired
    private HttpClientUtils hcu;

    @Value("${pk_org.code}")
    private Integer pkOrgCode;

    /**
     * 条件查询物料库存信息
     *
     * @Author wangzhichao
     * @Date 2024/12/11 15:08:54
     * @Param dto
     */
    @Override
    public List<NccBasicMaterialstock> findNccBasicMaterialstockData(NccBasicMaterialstockDto dto) {
        return nccBasicMaterialstockMapper.findNccBasicMaterialstockData(dto);
    }

    /**
     * 根据pkmaterialstock修改ncc物料库存仓库信息
     *
     * @Author wangzhichao
     * @Date 2024/12/11 15:19:36
     * @Param entity
     */
    @Override
    public AjaxResult nccBasicMaterialstockUpdate(NccBasicMaterialstock entity) {
        try {
            int i = nccBasicMaterialstockMapper.nccBasicMaterialstockUpdate(entity);
            return i > 0 ? AjaxResult.success("修改成功") : AjaxResult.error("未找到指定数据");
        } catch (Exception e) {
            log.error("修改失败:{}", e.getMessage());
            return AjaxResult.error("修改失败");
        }
    }

    /**
     * 新增ncc物料库存信息
     *
     * @Author wangzhichao
     * @Date 2024/12/11 15:32:14
     * @Param entity
     */
    @Override
    public AjaxResult nccBaiscMaterialstockSave(NccBasicMaterialstock entity) {
        String userName = SecurityUtils.getLoginUser().getUser().getUserName();
        String pkMaterialStock = "WMS" + uniq.generateUniqueIdentifier();
        entity.setPkmaterialstock(pkMaterialStock);
        entity.setCreator(userName);
        entity.setModifier(userName);
        entity.setCreationtime(new Date());
        entity.setModifiedtime(new Date());
        entity.setSysTime(new Date());
        entity.setPkorg("1001A110000000004AOQ");
        entity.setPkgroup("BEITEJITUAN123456789");

        try {
            nccBasicMaterialstockMapper.nccBaiscMaterialstockSave(entity);
            return AjaxResult.success("新增成功");
        } catch (Exception e) {
            log.error("新增失败:{}", e.getMessage());
            return AjaxResult.error("新增失败");
        }
    }

    /**
     * 定时任务同步当前数据
     *
     * @return
     */
    @Override
    public R jobMethodExecute(Boolean flag) {
        HashMap<String, String> searchDate = DataBatchProcessorUtil.getSearchDate(flag);

        // 查询当前数据
        HttpResponse response = hcu.methodExecute(searchDate.get("startTimeStr"), searchDate.get("endTimeStr"), 15, pkOrgCode);
        // 收集rep系统数据
        String responseBody = response.body();
        JSONObject entries = JSONObject.parseObject(responseBody);
        JSONArray dataArray = entries.getJSONArray("data");
        List<NccBasicMaterialstock> nccBasicMaterialstocks = dataArray.toList(NccBasicMaterialstock.class);
        if (!nccBasicMaterialstocks.isEmpty())
            extracted(nccBasicMaterialstocks);

        return R.ok("同步成功");
    }

    /**
     * 批量更新或者插入
     *
     * @Date 2024/12/11 14:17:12
     * @Param list
     */
    @Transactional(rollbackFor = Exception.class)
    public void extracted(List<NccBasicMaterialstock> list) {
        for (NccBasicMaterialstock nccBasicMaterialstock : list) {
            nccBasicMaterialstockMapper.insertOrUpdate(nccBasicMaterialstock);
        }
    }

}




